<script setup>
import {
  Edit,
  Delete, User
} from '@element-plus/icons-vue'
import { ref } from 'vue'
const supplier = ref([
  {   "id":"7",
    "supplierName":"钱大妈",
    "supplierAddress":"丽萨天下无敌萨迦天地总歇会",
    "supplierPhone":"1234678",
    "supplierEmail":"3526279811@qq.com",
    "postalCode":"Sc122323",
    "contactPerson":"我当时",
    "contactPhone":"21312412"
    ,"bankName":"天地银行",
    "bankAccount":"1312315",
    "status":"可用"
  }

])
const pageNum = ref(1)//当前页
const total = ref(20)//总条数
const pageSize = ref(3)//每页条数
const onSizeChange = (size) => {
  pageSize.value = size
  supplierList();
}
//当前页码发生变化，调用此函数
const onCurrentChange = (num) => {
  pageNum.value = num
  supplierList();
}
import {supplierListService,deleteSupplierService,updateSupplierService,AddSupplierService} from "@/api/supplier.js";
const supplierName = ref('')
const supplierList = async ()=>{
  let  params = {
    pageNum: pageNum.value,
    pageSize:pageSize.value,
    supplierName:supplierName.value?supplierName.value:null,
  }
  let result =  await  supplierListService(params);
  total.value =  result.data.total
  supplier.value = result.data.items;
}
supplierList();
import {Plus} from '@element-plus/icons-vue'
import {ElMessage, ElMessageBox} from "element-plus";
import {useTokenStore} from "@/Stores/token.js";
//控制抽屉是否显示
const visibleDrawer = ref(false)
//添加表单数据模型
const supplierModel = ref({
  id:'',
  supplierName: '',
  supplierAddress: '',
  supplierPhone: '',
  supplierEmail:'',
  postalCode:'',
  contactPerson:'',
  contactPhone:'',
  bankName:'',
  bankAccount:'',
  status:''
})
const title =ref('')
const tokenStore = useTokenStore()
const updateSupplierEcho =  (row)=>{
  title.value = '修改供应商'
  visibleDrawer.value = true
  supplierModel.value.id = row.id
  supplierModel.value.supplierName = row.supplierName
  supplierModel.value.supplierAddress = row.supplierAddress
  supplierModel.value.supplierPhone = row.supplierPhone
  supplierModel.value.supplierEmail = row.supplierEmail
  supplierModel.value.postalCode = row.postalCode
  supplierModel.value.contactPerson = row.contactPerson
  supplierModel.value.contactPhone = row.contactPhone
  supplierModel.value.bankName = row.bankName
  supplierModel.value.bankAccount = row.bankAccount
  supplierModel.value.status = row.status
}
const updateSupplier = async (state)=>{
  supplierModel.value.status = state
  let result =  await updateSupplierService(supplierModel.value);
  ElMessage.success(result.data?result.data:'修改成功')
  supplierList()
  visibleDrawer.value = false
}
const clearData = ()=>{
  supplierModel.value.id = '';
  supplierModel.value.supplierName = '';
  supplierModel.value.supplierAddress = '';
  supplierModel.value.supplierPhone = '';
  supplierModel.value.supplierEmail = '';
  supplierModel.value.postalCode = '';
  supplierModel.value.contactPerson = '';
  supplierModel.value.contactPhone = '';
  supplierModel.value.bankName = '';
  supplierModel.value.bankAccount = '';
  supplierModel.value.status = '';
}
const addSupplier = async (state) => {
  supplierModel.value.status = state
  let result = await AddSupplierService(supplierModel.value)
  ElMessage.success(result.data?result.data:'添加成功')
  supplierList()
  visibleDrawer.value = false
}
const deleteSupplier = (row) => {
  ElMessageBox.confirm(
      '确认删除该条供应商吗?',
      '温馨提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(async () => {
        let result = await deleteSupplierService(row.id);
        ElMessage({
          type: 'success',
          message: '确认删除',
        })
        customerList()
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '取消了删除',
        })
      })
}
</script>
<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>供应商列表</span>
        <div class="extra">
          <el-button type="primary" @click="visibleDrawer=true;title='添加供应商';clearData()">添加供应商</el-button>
        </div>
      </div>
    </template>
    <el-form inline>
      <el-form-item label="供应商列表:">
        <el-input  placeholder="请输入供应商名" v-model="supplierName"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="supplierList">搜索</el-button>
        <el-button @click="supplierName='';supplierList()">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="supplier" style="width: 100%">
      <el-table-column label="序号" width="100" type="index"> </el-table-column>
      <el-table-column label="供应商名称" prop="supplierName"></el-table-column>
      <el-table-column label="供应商地址" prop="supplierAddress"></el-table-column>
      <el-table-column label="供应商电话" prop="supplierPhone"></el-table-column>
      <el-table-column label="供应商邮箱" prop="supplierEmail"></el-table-column>
      <el-table-column label="供应商邮编" prop="postalCode"></el-table-column>
      <el-table-column label="联系人" prop="contactPerson"></el-table-column>
      <el-table-column label="联系人电话" prop="contactPhone"></el-table-column>
      <el-table-column label="开户银行" prop="bankName"></el-table-column>
      <el-table-column label="开户账号" prop="bankAccount"></el-table-column>
      <el-table-column label="状态" prop="status"></el-table-column>
      <el-table-column label="操作" width="100">
        <template #default="{ row }">
          <el-button :icon="Edit" circle plain type="primary" @click="updateSupplierEcho(row)" ></el-button>
          <el-button :icon="Delete" circle plain type="danger" @click="deleteSupplier(row)"></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <el-pagination v-model:current-page="pageNum" v-model:page-size="pageSize" :page-sizes="[3, 5 ,10, 15]"
                   layout="jumper, total, sizes, prev, pager, next" background :total="total" @size-change="onSizeChange"
                   @current-change="onCurrentChange" style="margin-top: 20px; justify-content: flex-end" />

    <el-drawer v-model="visibleDrawer" :title="title" direction="rtl" size="50%">
      <!-- 添加文章表单 -->
      <el-form :model="supplierModel" label-width="100px" >
        <el-form-item label="供应商名称" >
          <el-input v-model="supplierModel.supplierName" placeholder="请输入供应商名"></el-input>
        </el-form-item>
        <el-form-item label="供应商地址" >
          <el-input v-model="supplierModel.supplierAddress" placeholder="请输入供应商地址"></el-input>
        </el-form-item>
        <el-form-item label="供应商电话" >
          <el-input v-model="supplierModel.supplierPhone" placeholder="请输入供应商电话"></el-input>
        </el-form-item>
        <el-form-item label="供应商邮箱" >
          <el-input v-model="supplierModel.supplierEmail" placeholder="请输入供应商邮箱"></el-input>
        </el-form-item>
        <el-form-item label="供应商邮编" >
          <el-input v-model="supplierModel.postalCode" placeholder="请输入供应商邮编"></el-input>
        </el-form-item>
        <el-form-item label="供应商联系人" >
          <el-input v-model="supplierModel.contactPerson" placeholder="请输入联系人"></el-input>
        </el-form-item>
        <el-form-item label="供应商联系人电话" >
          <el-input v-model="supplierModel.contactPhone" placeholder="请输入联系人电话"></el-input>
        </el-form-item>
        <el-form-item label="供应商开户账号" >
          <el-input v-model="supplierModel.bankAccount" placeholder="请输入供应商开户账号"></el-input>
        </el-form-item>
        <el-form-item label="供应商开户银行" >
          <el-input v-model="supplierModel.bankName" placeholder="请输入客户开户银行"></el-input>
        </el-form-item>
        <!--        <el-form-item label="文章分类">-->
        <!--          <el-select placeholder="请选择" v-model="articleModel.categoryId">-->
        <!--            <el-option v-for="c in customers" :key="c.id" :label="c.customerName" :value="c.id">-->
        <!--            </el-option>-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
        <el-form-item>
          <el-button type="primary" @click="title==='添加供应商'?addSupplier('可用'):updateSupplier('可用')">可用</el-button>
          <el-button type="info" @click="title==='添加供应商'?addSupplier('不可用'):updateSupplier('不可用')">不可用</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </el-card>

</template>

<style lang="scss" scoped>
.page-container {
  margin-left: 5px;
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
</style>
